package com.kakaxi.mybilibili.adapter;

import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.daimajia.slider.library.SliderLayout;
import com.kakaxi.mybilibili.R;
import com.kakaxi.mybilibili.adapter.entity.CategoryAreaFirstMultiItem;
import com.kakaxi.mybilibili.bean.BannerItemBean;
import com.kakaxi.mybilibili.bean.CategoryAreaVideoItemBean;
import com.kakaxi.mybilibili.utils.CastUncheckedUtil;
import com.kakaxi.mybilibili.utils.SliderLayoutHelper;
import com.kakaxi.mybilibili.view.GridMarginDecoration;

import java.util.List;

/**
 * 分类分区首个碎片列表适配器
 * Created by kakaxi on 16/11/28.
 */

public class CategoryAreaFirstRecyAdapter extends
        BaseMultiItemQuickAdapter<CategoryAreaFirstMultiItem, BaseViewHolder> {

    /** 轮播是否初始化 **/
    private boolean bannerIsNotInit = true;

    public CategoryAreaFirstRecyAdapter(List<CategoryAreaFirstMultiItem> data) {
        super(data);
        addItemType(CategoryAreaFirstMultiItem.TYPE_HEADER_BANNER, R.layout
                .item_recy_common_banner);
        addItemType(CategoryAreaFirstMultiItem.TYPE_HEADER_BUTTON_BAR, R.layout
                .item_recy_category_area_first_btn_bar);
        addItemType(CategoryAreaFirstMultiItem.TYPE_HEADER_RECOMMEND, R.layout
                .item_recy_advice_home_header);
        addItemType(CategoryAreaFirstMultiItem.TYPE_HEADER_NEW, R.layout
                .item_recy_advice_home_header);
        addItemType(CategoryAreaFirstMultiItem.TYPE_HEADER_ALL, R.layout
                .item_recy_advice_home_header);
        addItemType(CategoryAreaFirstMultiItem.TYPE_CONTENT, R.layout
                .item_recy_advice_home_content);
    }

    @Override
    protected void convert(BaseViewHolder baseViewHolder, CategoryAreaFirstMultiItem
            categoryAreaFirstMultiItem) {
        int itemType = categoryAreaFirstMultiItem.getItemType();
        Object data = categoryAreaFirstMultiItem.getData();
        switch (itemType) {
            case CategoryAreaFirstMultiItem.TYPE_HEADER_BANNER: // 轮播
                onBindHeaderBannerViewHolder(baseViewHolder, data);
                break;
            case CategoryAreaFirstMultiItem.TYPE_HEADER_BUTTON_BAR: // 按钮组
                onBindHeaderButtonBarViewHolder(baseViewHolder, data);
                break;
            case CategoryAreaFirstMultiItem.TYPE_HEADER_RECOMMEND: // 热门推荐
                onBindHeaderRecommendViewHolder(baseViewHolder, data);
                break;
            case CategoryAreaFirstMultiItem.TYPE_HEADER_NEW: // 最新视频
                onBindHeaderNewViewHolder(baseViewHolder, data);
                break;
            case CategoryAreaFirstMultiItem.TYPE_HEADER_ALL: // 全区动态
                onBindHeaderAllViewHolder(baseViewHolder, data);
                break;
            case CategoryAreaFirstMultiItem.TYPE_CONTENT: // 正文
                onBindContentViewHolder(baseViewHolder, data);
                break;
        }
    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
        if (recyclerView.getLayoutManager() == null) {
            // 设置格子布局管理器
            GridLayoutManager layoutManager = new GridLayoutManager(recyclerView.getContext(), 2);
            // 设置单列和满列
            layoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
                @Override
                public int getSpanSize(int position) {
                    int spanSize = 1;
                    switch (getItemViewType(position)) {
                        case CategoryAreaFirstMultiItem.TYPE_HEADER_BANNER:
                        case CategoryAreaFirstMultiItem.TYPE_HEADER_BUTTON_BAR:
                        case CategoryAreaFirstMultiItem.TYPE_HEADER_RECOMMEND:
                        case CategoryAreaFirstMultiItem.TYPE_HEADER_NEW:
                        case CategoryAreaFirstMultiItem.TYPE_HEADER_ALL:
                            spanSize = 2;
                            break;
                    }
                    return spanSize;
                }
            });
            recyclerView.setLayoutManager(layoutManager);
        }
        // 设置正文的边距
        recyclerView.addItemDecoration(new GridMarginDecoration(recyclerView.getContext(),
                GridMarginDecoration.DEFAULT_MARGIN_DP, CategoryAreaFirstMultiItem.TYPE_CONTENT));
    }

    /**
     * 绑定轮播数据
     *
     * @param baseViewHolder 持久化视图
     * @param data           数据
     */
    private void onBindHeaderBannerViewHolder(BaseViewHolder baseViewHolder, Object data) {
        // 初始化轮播
        SliderLayout sliderLayout = baseViewHolder.getView(R.id.sl_common_banner);
        if (bannerIsNotInit) {
            SliderLayoutHelper.initSliderLayout(sliderLayout);
            bannerIsNotInit = false;
        }
        // 设置轮播数据
        List<BannerItemBean> bannerItemBeanList = CastUncheckedUtil.cast(data);
        SliderLayoutHelper.setUrlSlider(sliderLayout, bannerItemBeanList, null);
    }

    /**
     * 绑定按钮组数据
     *
     * @param baseViewHolder 持久化视图
     * @param data           数据
     */
    private void onBindHeaderButtonBarViewHolder(BaseViewHolder baseViewHolder, Object data) {

    }

    /**
     * 绑定热门推荐标题数据
     *
     * @param baseViewHolder 持久化视图
     * @param data           数据
     */
    private void onBindHeaderRecommendViewHolder(BaseViewHolder baseViewHolder, Object data) {
        String title = (String) data;
        baseViewHolder
                .setText(R.id.txt_advice_home_header_name, title)
                .setText(R.id.roubtn_advice_home_header_go, "排行榜");

        GridLayoutManager.LayoutParams layoutParams = (GridLayoutManager.LayoutParams)
                baseViewHolder.itemView.getLayoutParams();
        layoutParams.topMargin = (int) mContext.getResources().getDimension(R.dimen
                .default_distance);
    }

    /**
     * 绑定最新视频标题数据
     *
     * @param baseViewHolder 持久化视图
     * @param data           数据
     */
    private void onBindHeaderNewViewHolder(BaseViewHolder baseViewHolder, Object data) {
        String title = (String) data;
        baseViewHolder
                .setText(R.id.txt_advice_home_header_name, title)
                .setText(R.id.roubtn_advice_home_header_go, "进去看看");
    }

    /**
     * 绑定全区动态标题数据
     *
     * @param baseViewHolder 持久化视图
     * @param data           数据
     */
    private void onBindHeaderAllViewHolder(BaseViewHolder baseViewHolder, Object data) {
        String title = (String) data;
        baseViewHolder
                .setText(R.id.txt_advice_home_header_name, title);
        baseViewHolder.getView(R.id.roubtn_advice_home_header_go).setVisibility(View.INVISIBLE);
        baseViewHolder.itemView.setClickable(false);
    }

    /**
     * 绑定正文数据
     *
     * @param baseViewHolder 持久化视图
     * @param data           数据
     */
    private void onBindContentViewHolder(BaseViewHolder baseViewHolder, Object data) {
        CategoryAreaVideoItemBean itemBean = (CategoryAreaVideoItemBean) data;
        baseViewHolder
                .setText(R.id.txt_advice_home_content_title, itemBean.getTitle())
                .setText(R.id.txt_advice_home_content_play, itemBean.getPlay())
                .setText(R.id.txt_advice_home_content_danmaku, itemBean.getVideoReview() + "");
        Glide.with(mContext).load(itemBean.getPic()).crossFade().into((ImageView)
                baseViewHolder.getView(R.id.srimg_advice_home_content_cover));
    }


}
